package com.qu.waibao.utils;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import java.lang.reflect.Field;

/**
 * Created by quguangle on 2018/7/21.
 * email 1123599561@qq.com
 */

public class DeviceScreen {
    public static float[] getScreenWidthandHeight(Application application){
        float[] sizes = new float[2];
        PackageManager packageManager = application.getPackageManager();
        ApplicationInfo applicationInfo = null;
        try {
            applicationInfo = packageManager.getApplicationInfo(application.getPackageName(),
                    PackageManager.GET_META_DATA);

            if(applicationInfo != null && applicationInfo.metaData != null &&
                    applicationInfo.metaData.containsKey("fit_width") &&
                    applicationInfo.metaData.containsKey("fit_height")){
                String width = String.valueOf(applicationInfo.metaData.getInt("fit_width",360));
                String height = String.valueOf(applicationInfo.metaData.getInt("fit_height",640));
                sizes[0] = Float.parseFloat(width);
                sizes[1] = Float.parseFloat(height);

            }
        }catch (PackageManager.NameNotFoundException e){

        }
        return sizes;
    }

    public static int getStatusBarHeight(Application application) {
        Class<?> c = null;
        Object obj = null;
        Field field = null;
        int x = 0;
        try {
            c = Class.forName("com.android.internal.R$dimen");
            obj = c.newInstance();
            field = c.getField("status_bar_height");
            x = Integer.parseInt(field.get(obj).toString());
            return application.getResources()
                    .getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}
